package com.zfzs.post.modules.excel.service.impl;

import com.aliyun.oss.common.utils.DateUtil;
import com.zfzs.post.exception.BadRequestException;
import com.zfzs.post.exl.ExlUtils;
import com.zfzs.post.modules.agent.domain.Agent;
import com.zfzs.post.modules.agent.service.AgentService;
import com.zfzs.post.modules.excel.service.ExlService;
import com.zfzs.post.modules.post.domain.PostTerminalDistribu;
import com.zfzs.post.modules.post.domain.TerminalInfo;
import com.zfzs.post.modules.post.service.PostTerminalDistribuService;
import com.zfzs.post.modules.post.service.TerminalInfoService;
import com.zfzs.post.modules.transaction.domain.TransactionInfo;
import com.zfzs.post.modules.transaction.service.TransactionInfoService;
import com.zfzs.post.utils.DateStrUtils;
import com.zfzs.post.utils.SecurityUtils;
import com.zfzs.post.utils.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 上传
 *
 * @author ck
 * @date 2019-12-04
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ExcelServiceImpl implements ExlService {

    @Autowired
    private TerminalInfoService terminalInfoService;
    @Autowired
    private AgentService agentService;
    @Autowired
    private PostTerminalDistribuService distribuService;
    @Autowired
    private TransactionInfoService transactionInfoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int uploadTerminalExcel(MultipartFile attach, Long terminalId) {
        //解析模板数据
        Workbook wb = ExlUtils.getWorkbook(attach);
        List<String[]> dataList = new ArrayList<>();
        List<TerminalInfo> list = new LinkedList<>();
        Long channelId = SecurityUtils.getUserId();
        //查询顶级账号
        Agent agent = agentService.queryAgentTop(channelId);
        if (agent == null) {
            throw new BadRequestException("未查询到顶级账号信息");
        }
        dataList = ExlUtils.getAllData(0, wb);
        //默认每列去一个值
        for (int i = 1; i < dataList.size(); i++) {
            String[] row = dataList.get(i);
            TerminalInfo terminalInfo = new TerminalInfo();
            //默认每列去一个值 开始导入数据
            String code = row[0]; //终端号
            String sn = row[1]; //SN号
            if (StringUtils.isEmpty(code) || StringUtils.isEmpty(sn)) {
                throw new BadRequestException("导入数据不能有空值");
            }
            //查询终端是否存在
            TerminalInfo info = terminalInfoService.findBySN(sn);
            if (info != null) {
                throw new BadRequestException("数据导入失败，SN号<" + sn + ">已经存在");
            }
            Long agentId = agent.getId();
            terminalInfo.setAgentId(agentId);
            terminalInfo.setChannelId(channelId);
            terminalInfo.setSn(sn);
            terminalInfo.setNumbers(code);
            terminalInfo.setTerminalTypeId(terminalId);
            list.add(terminalInfo);
        }
        //开始录入数据
        for (int i = 0; i < list.size(); i++) {
            terminalInfoService.create(list.get(i));
            //录入关联信息
            PostTerminalDistribu distribu = new PostTerminalDistribu();
            distribu.setSn(list.get(i).getSn());
            distribu.setState(2);
            distribu.setChannelId(channelId);
            distribu.setAgentId(list.get(i).getAgentId());
            distribuService.create(distribu);
        }
        return list.size();
    }

    @Override
    public int uploadTransactionExcel(MultipartFile attach, Long terminalId) {
        //解析模板数据
        Workbook wb = ExlUtils.getWorkbook(attach);
        List<String[]> dataList = new ArrayList<>();
        List<TransactionInfo> list = new LinkedList<>();
        Long channelId = SecurityUtils.getUserId();
        dataList = ExlUtils.getAllData(0, wb);
        //默认每列去一个值
        for (int i = 1; i < dataList.size(); i++) {
            String[] row = dataList.get(i);
            TransactionInfo transactionInfo = new TransactionInfo();
            //默认每列去一个值 开始导入数据
            String merchantCode = row[0]; //商户号
            String amount = row[1]; //总交易额
            String rateAmount = row[2]; //手续费
            String transactionDate = row[3]; //交易时间
            if (StringUtils.isEmpty(merchantCode) || StringUtils.isEmpty(rateAmount) || StringUtils.isEmpty(amount) || StringUtils.isEmpty(transactionDate)) {
                throw new BadRequestException("导入数据不能有空值");
            }
            //查询终端是否存在
            TerminalInfo info = terminalInfoService.findByMerchantCode(merchantCode);
            if (info == null) {
                throw new BadRequestException("数据导入失败，商户号<" + merchantCode + ">的终端不存在");
            }
            transactionInfo.setChannelId(channelId);
            transactionInfo.setSn(info.getSn());
            transactionInfo.setAmount(new BigDecimal(amount));
            transactionInfo.setTerminalTypeId(terminalId);
            transactionInfo.setMerchantCode(merchantCode);
            transactionInfo.setRateAmount(new BigDecimal(rateAmount));
            transactionInfo.setTransactionTime(DateStrUtils.parseDate(transactionDate,DateStrUtils.Y_M_D));
            list.add(transactionInfo);
        }
        //开始录入数据
        for (int i = 0; i < list.size(); i++) {
            transactionInfoService.create(list.get(i));
        }
        return list.size();
    }
}